package com.khoayang.littlefighterchaos.menu;

import java.util.ArrayList;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor3B;

import com.khoayang.littlefighterchaos.game.BackgoundGame;
import com.khoayang.littlefighterchaos.game.GameManagerContent;
import android.content.Context;
import android.view.MotionEvent;

public class MainMenuLayer extends CCLayer{
	Context _Context;
	
	ArrayList<MenuButton> _ListMenuButton;
	MenuButton _NewGameButton;
	MenuButton _OptionButton;
	MenuButton _AboutButton;
	MenuButton _ExitButton;
	
	BaseButton _Back;
	BaseButton _OnSound;
	BaseButton _OffSound;
	BaseButton _OKSound;
	BaseButton _VSMODE;
	BaseButton _STAGEMODE;
	CCLabel _LabelGame;
	CCLabel _LabelDeveloper1;
	CCLabel _LabelDeveloper2;

	BaseButton _NormalLevel;
	BaseButton _HardLevel;
	
	public static int _DifficultyLevel = 0;
	public static boolean _isStageMode;
	public MainMenuLayer(Context context){
		this.setIsTouchEnabled(true);
		_Context = context;
		CGSize winSize = CCDirector.sharedDirector().displaySize();
		_ListMenuButton = new ArrayList<MenuButton>();
		
		//initial all button
		_NewGameButton = new MenuButton();
		_NewGameButton.set_PosY(winSize.height - 150);
		_NewGameButton.set_Layer(this);
		_NewGameButton.set_Title("NEW GAME");
		_NewGameButton.Draw();
		_ListMenuButton.add(_NewGameButton);
		
		_OptionButton = new MenuButton();
		_OptionButton.set_PosY(winSize.height - 250);
		_OptionButton.set_Layer(this);
		_OptionButton.set_Title("OPTION");
		_OptionButton.Draw();
		_ListMenuButton.add(_OptionButton);
		
		_AboutButton = new MenuButton();
		_AboutButton.set_PosY(winSize.height - 350);
		_AboutButton.set_Layer(this);
		_AboutButton.set_Title("ABOUT");
		_AboutButton.Draw();
		_ListMenuButton.add(_AboutButton);
		
		_ExitButton = new MenuButton();
		_ExitButton.set_PosY(winSize.height - 450);
		_ExitButton.set_Layer(this);
		_ExitButton.set_Title("EXIT");
		_ExitButton.Draw();
		_ListMenuButton.add(_ExitButton);
		
		_Back = new BaseButton();
		_Back.set_PosY(winSize.height - 500);
		_Back.set_Layer(this);
		_Back.set_Title("BACK");
		_AboutButton.add(_Back);
		
		_OnSound = new BaseButton();
		_OnSound.set_PosY(winSize.height - 300);
		_OnSound.set_PosX(winSize.width/2-200);
		_OnSound.set_Layer(this);
		_OnSound.set_Title("On Sound");
		_OptionButton.add(_OnSound);
		
		_OffSound = new BaseButton();
		_OffSound.set_PosY(winSize.height - 300);
		_OffSound.set_PosX(winSize.width/2 + 200);
		_OffSound.set_Layer(this);
		_OffSound.set_Title("Off Sound");
		_OptionButton.add(_OffSound);
		
		_NormalLevel = new BaseButton();
		_NormalLevel.set_PosY(winSize.height - 400);
		_NormalLevel.set_PosX(winSize.width/2-200);
		_NormalLevel.set_Layer(this);
		_NormalLevel.set_Title("Normal Level");
		_OptionButton.add(_NormalLevel);
		
		_HardLevel = new BaseButton();
		_HardLevel.set_PosY(winSize.height - 400);
		_HardLevel.set_PosX(winSize.width/2 + 200);
		_HardLevel.set_Layer(this);
		_HardLevel.set_Title("Hard Level");
		_OptionButton.add(_HardLevel);

		_OKSound = new BaseButton();
		_OKSound.set_PosY(winSize.height - 500);
		_OKSound.set_Layer(this);
		_OKSound.set_Title("BACK");
		_OptionButton.add(_OKSound);
		
		_VSMODE = new BaseButton();
		_VSMODE.set_PosY(winSize.height - 200);
		_VSMODE.set_Layer(this);
		_VSMODE.set_Title("VS MODE");
		_NewGameButton.add(_VSMODE);
		
		_STAGEMODE = new BaseButton();
		_STAGEMODE.set_PosY(winSize.height - 400);
		_STAGEMODE.set_Layer(this);
		_STAGEMODE.set_Title("STAGE MODE");
		_NewGameButton.add(_STAGEMODE);
	}
	
	
	@Override
	public boolean ccTouchesMoved(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		if(_ListMenuButton.get(0).get_Enable()==true)
		for(int i=0;i< _ListMenuButton.size();i++)
		{
			if(_ListMenuButton.get(i).checkTouch(x, y))
			{
				_ListMenuButton.get(i).get_Sprite().setOpacity(255);
			}
			else
			{
				_ListMenuButton.get(i).get_Sprite().setOpacity(128);
			}
		}
		
		if(_AboutButton.get_ListControl().get(0).get_Enable()==true)
		for(int i=0;i< _AboutButton.get_ListControl().size();i++)
		{
			if(_AboutButton.get_ListControl().get(i).checkTouch(x, y))
			{
				_AboutButton.get_ListControl().get(i).get_Sprite().setOpacity(255);
			}
			else
			{
				_AboutButton.get_ListControl().get(i).get_Sprite().setOpacity(128);
			}
		}
		
		if(_OptionButton.get_ListControl().get(0).get_Enable()==true)
		for(int i=0;i< _OptionButton.get_ListControl().size();i++)
		{
			if(_OptionButton.get_ListControl().get(i).checkTouch(x, y))
			{
				_OptionButton.get_ListControl().get(i).get_Sprite().setOpacity(255);
			}
			else
			{
				_OptionButton.get_ListControl().get(i).get_Sprite().setOpacity(128);
			}
		}
		return super.ccTouchesMoved(event);
	}
	
	@Override
	public boolean ccTouchesBegan(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		if(_ListMenuButton.get(0).get_Enable()==true)
		for(int i=0;i< _ListMenuButton.size();i++)
		{
			if(_ListMenuButton.get(i).checkTouch(x, y))
			{
				_ListMenuButton.get(i).get_Sprite().setOpacity(255);
			}
		}
		
		return super.ccTouchesBegan(event);
	}
	
	@Override
	public boolean ccTouchesEnded(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		if(_ExitButton.checkTouch(x, y) && _ExitButton.get_Enable() == true)
		{
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		if(_NewGameButton.checkTouch(x, y) && _NewGameButton.get_Enable() == true)
		{
			
			_NewGameButton.show();
			
			hide();
			return true;
		}
		if(_VSMODE.checkTouch(x, y) && _VSMODE.get_Enable() == true)
		{
			_isStageMode = false;
			_VSMODE.get_ParentControl().hide();
			
			ChooseMap chooseMap = new ChooseMap(_Context);
			addChild(chooseMap);
			return true;
		}
		if(_STAGEMODE.checkTouch(x, y) && _STAGEMODE.get_Enable() == true)
		{
			_isStageMode = true;
			_STAGEMODE.get_ParentControl().hide();
			
			BackgoundGame.ground1 = "background-game/ground1.png";
			BackgoundGame.ground2 = "background-game/ground1.png";
			BackgoundGame.ground3 = "background-game/ground1.png";
			BackgoundGame.sky = "background-game/sky1.jpg";
			
			ChooseCharacterLayer chooseCharacterLayer = new ChooseCharacterLayer(_Context);
			addChild(chooseCharacterLayer);
			return true;
		}
		if(_AboutButton.checkTouch(x, y) && _AboutButton.get_Enable() == true)
		{
			CGSize winSize = CCDirector.sharedDirector().winSize();
	        _AboutButton.show();
	        _LabelGame = CCLabel.makeLabel("Hero Fighter", "DroidSans", 30);
	        _LabelGame.setColor(ccColor3B.ccRED);
	        _LabelGame.setPosition(winSize.width/2, winSize.height/3*2);
			addChild(_LabelGame);
			_LabelDeveloper1 = CCLabel.makeLabel("0912577 -  Huỳnh Đăng Khoa","DroidSans", 30);
			_LabelDeveloper1.setColor(ccColor3B.ccRED);
			_LabelDeveloper1.setPosition(winSize.width/2, winSize.height/3*2-50);
			addChild(_LabelDeveloper1);
			_LabelDeveloper2 = CCLabel.makeLabel("0912093 - Trần Thái Dương","DroidSans", 30);
			_LabelDeveloper2.setColor(ccColor3B.ccRED);
			_LabelDeveloper2.setPosition(winSize.width/2, winSize.height/3*2-100);
			addChild(_LabelDeveloper2);
	        hide();
	        return true;
		}
		if(_Back.checkTouch(x, y) && _Back.get_Enable() == true)
		{
			_LabelGame.removeSelf();
			_LabelDeveloper1.removeSelf();
			_LabelDeveloper2.removeSelf();
			_Back.get_ParentControl().hide();
			show();
			return true;
		}
		if(_OptionButton.checkTouch(x, y) && _OptionButton.get_Enable() == true)
		{
			_OptionButton.show();
		    hide();
		    return true;
		}
		if(_OKSound.checkTouch(x, y) && _OKSound.get_Enable() == true)
		{
			_OKSound.get_ParentControl().hide();
			show();
			return true;
		}
		if(_OnSound.checkTouch(x, y) && _OnSound.get_Enable() == true)
		{
			GameManagerContent.Instance().set_UseEffectSound(true);
			//SoundEngine.sharedEngine().unmute();
			SoundEngine.sharedEngine().setSoundVolume(1f);
			SoundEngine.sharedEngine().setEffectsVolume(1f);
			return true;
		}
		if(_OffSound.checkTouch(x, y) && _OffSound.get_Enable() == true)
		{
			GameManagerContent.Instance().set_UseEffectSound(false);
			//SoundEngine.sharedEngine().mute();
			SoundEngine.sharedEngine().setSoundVolume(0f);
			SoundEngine.sharedEngine().setEffectsVolume(0f);
			return true;
		}
		if(_NormalLevel.checkTouch(x, y) && _NormalLevel.get_Enable() == true)
		{
			_DifficultyLevel = 0;
			return true;
		}
		if(_HardLevel.checkTouch(x, y) && _HardLevel.get_Enable() == true)
		{
			_DifficultyLevel = 1;
			return true;
		}
		return super.ccTouchesEnded(event);
	}
	
	public void hide()
	{
		for(int i=0;i< _ListMenuButton.size();i++)
		{
			_ListMenuButton.get(i).UnDraw();
		}
	}
	
	public void show()
	{
		for(int i=0;i< _ListMenuButton.size();i++)
		{
			_ListMenuButton.get(i).Draw();
		}
	}
}
